package com.allenkerr.client.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;

import static com.allenkerr.filter.MdcFilter.REQUEST_UUID_KEY;

/**
 * @author 小柯
 * @date 2019/1/15
 */
public class MdcRequestInterceptor implements RequestInterceptor {

  @Override
  public final void apply(RequestTemplate template) {
    String requestUuid = MDC.get(REQUEST_UUID_KEY);
    if (requestUuid != null) {
      template.header(REQUEST_UUID_KEY, requestUuid);
    }
    andApply(template);
  }

  /**
   * Subclasses should rewrite this method for adding specific data into {@link RequestTemplate}.
   * <p>
   * 子类需要重写该方法，用于添加特定的数据到 {@link RequestTemplate} 中。
   *
   * @param template must not be {@literal null}.
   */
  protected void andApply(RequestTemplate template) {
  }

}
